class Course < ActiveRecord::Base
  belongs_to :term
  has_many :lectures, :dependent=>:destroy
  has_many :discussions, :dependent=>:destroy
  has_many :labs, :dependent=>:destroy
  validates_numericality_of :units, :greater_than_or_equal_to => 0, :allow_nil => true
  validates_numericality_of :term_id, :greater_than => 0
  validates_presence_of :course_number

  def name
    return department + ' ' + course_number
  end

  def Course.update_course(fields)
    return nil if not fields[:term_id] or not fields[:department] or not fields[:course_number]
    if count(:all, :conditions => {
      :term_id => fields[:term_id],
      :department => fields[:department],
      :course_number => fields[:course_number]
      }) > 0
      c = find(:first, :conditions => {
        :term_id => fields[:term_id],
        :department => fields[:department],
        :course_number => fields[:course_number]
        })
      c[:title] = fields[:title] if fields[:title]
      c[:description] = fields[:description] if fields[:description]  
      c[:units] = fields[:units] if fields[:units]
      c.save
      return c
    else
      return Course.create!(fields)
    end
  end

end
